package com.project.market.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.project.market.db.domain.MarketIssue;
import com.project.market.db.domain.MarketIssueExample;
import com.project.market.db.domain.MarketKeyword;
import com.project.market.db.domain.MarketKeywordExample;
import com.project.market.db.mapper.MarketIssueMapper;
import com.project.market.db.mapper.MarketKeywordMapper;
import com.project.market.service.AdminKeywordService;
import com.project.market.util.MybatisUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;
import javax.swing.text.DateFormatter;
import java.text.DateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class AdminKeywordServiceImpl implements AdminKeywordService {


    /*
    查询操作
     */
    @Override
    public List<MarketKeyword> list(String page, String limit, String keyword, String url, String sort, String order) {

        // 获取session对象
        SqlSession session = MybatisUtil.getSession();

        // 创建mapper对象
        MarketKeywordMapper mapper = session.getMapper(MarketKeywordMapper.class);

        // 创建example对象
        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();

        // 添加条件
        MarketKeywordExample.Criteria criteria = marketKeywordExample.createCriteria();

        criteria.andDeletedEqualTo(false);

        // 查询
        if (keyword != null) {
            criteria.andKeywordLike("%" + keyword + "%");
        }
        if (url != null) {
            criteria.andUrlLike("%" + url + "%");
        }

        // 根据sort 降序排列
        marketKeywordExample.setOrderByClause(sort + " " + order);

        // 分页
        Page<Object> startPage = PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(limit));

        List<MarketKeyword> list = mapper.selectByExample(marketKeywordExample);

        session.commit();
        session.close();
        return list;
    }

    /*
    添加操作
     */
    @Override
    public Map<String, Object> create(String isDefault, String isHot, String keyword, String url) {

        // 获取session对象
        SqlSession session = MybatisUtil.getSession();

        // 创建mapper对象
        MarketKeywordMapper mapper = session.getMapper(MarketKeywordMapper.class);

        // 创建example对象
        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();

        // 添加条件
        MarketKeywordExample.Criteria criteria = marketKeywordExample.createCriteria();

        // 创建marketKeyword对象
        MarketKeyword marketKeyword = new MarketKeyword();
        // 设置变量
        marketKeyword.setKeyword(keyword);

        // 如果url为null,设置为空字符串
        if (StringUtils.isEmpty(url)) {
            marketKeyword.setUrl("");
        }else {
            marketKeyword.setUrl(url);
        }

        // 默认为false
        Boolean isHot1 = false;
        if (!StringUtils.isEmpty(isHot)) {
            isHot1 = Boolean.getBoolean(isHot);
        }
        marketKeyword.setIsHot(isHot1);

        // 默认为false
        Boolean isDefault1 = false;
        if (!StringUtils.isEmpty(isDefault)) {
            isDefault1 = Boolean.getBoolean(isDefault);
        }
        marketKeyword.setIsDefault(isDefault1);

        // sort-order 设置为100
        marketKeyword.setSortOrder(100);

        marketKeyword.setAddTime(LocalDateTime.now());
        marketKeyword.setUpdateTime(LocalDateTime.now());
        marketKeyword.setDeleted(false);

        // 插入操作
        mapper.insert(marketKeyword);

        // 向map里封装信息
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("id", marketKeyword.getId());
        map.put("keyword", marketKeyword.getKeyword());

        if (!StringUtils.isEmpty(url)) {
            map.put("url", marketKeyword.getUrl());
        }

        if (!StringUtils.isEmpty(isHot)) {
            map.put("isHot", marketKeyword.getIsHot());
        }

        if (!StringUtils.isEmpty(isDefault)) {
            map.put("isDefault", marketKeyword.getIsDefault());
        }

        map.put("addTime", marketKeyword.getAddTime());
        map.put("updateTime", marketKeyword.getUpdateTime());

        // 提交关闭session
        session.commit();
        session.close();

        return map;
    }

    /*
    更新操作
     */
    @Override
    public MarketKeyword delete(String addTime, Integer id, String keyword, String updateTime) {

        // 获取session对象
        SqlSession session = MybatisUtil.getSession();

        // 创建mapper对象
        MarketKeywordMapper mapper = session.getMapper(MarketKeywordMapper.class);

        // 创建example对象
        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();

        // 添加条件
        MarketKeywordExample.Criteria criteria = marketKeywordExample.createCriteria();

        criteria.andIdEqualTo(id);

        MarketKeyword marketKeyword = mapper.selectByPrimaryKey(id);
        marketKeyword.setDeleted(true);
        marketKeyword.setUpdateTime(LocalDateTime.now());

        mapper.updateByExample(marketKeyword, marketKeywordExample);
        session.commit();
        session.close();
        return marketKeyword;
    }

    @Override
    public MarketKeyword update(String addTime, Integer id, String keyword, String updateTime, String url) {

        SqlSession session = MybatisUtil.getSession();
        MarketKeywordMapper mapper = session.getMapper(MarketKeywordMapper.class);
        // 创建example对象
        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();

        MarketKeywordExample.Criteria criteria = marketKeywordExample.createCriteria();
        criteria.andIdEqualTo(id);

        MarketKeyword marketKeyword = mapper.selectByPrimaryKey(id);


        marketKeyword.setKeyword(keyword);
        marketKeyword.setUrl(url);

        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        marketKeyword.setAddTime(LocalDateTime.parse(addTime,df));
        marketKeyword.setUpdateTime(LocalDateTime.now());

        mapper.updateByExample(marketKeyword,marketKeywordExample);

        // 关闭session
        session.commit();
        session.close();

        return marketKeyword;
    }
}
